package org.bee.service;

import org.bee.enums.IsDelete;
import org.bee.input.DepartmentSearchVo;
import org.bee.mappers.DepartmentMapper;
import org.bee.model.Department;
import org.bee.model.DepartmentExample;
import org.bee.output.DepartmentTree;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈国松
 * @date 2024-08-03 17:31:46
 * @description 部门表Service层
 */
@Service
public class DepartmentService{

    private final DepartmentMapper departmentMapper;

    public DepartmentService(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }

    /**
     * 插入部门表
     * @param department 部门表实体
     * @return 插入结果
     */
    public int insert(Department department) {
        return departmentMapper.insert(department);
    }

    /**
     * 删除部门表
     * @param departmentId 部门表id
     * @return 删除结果
     */
    public int delete(String departmentId,Boolean isDelete) {
        if(isDelete){
            return departmentMapper.deleteByPrimaryKey(departmentId);
        }else{
            Department department = new Department();
            department.setId(departmentId);
            department.setIsDeleted(true);
            return departmentMapper.updateByPrimaryKeySelective(department);
        }
    }

    /**
     * 根据主键更新部门表
     * @param department 部门表实体
     * @return 更新结果
     */
    public int update(Department department) {
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    /**
    * 查询部门表
    * @param departmentSearchVo 部门表查询参数
    * @return 部门表列表
    */
    public List<Department> select(DepartmentSearchVo departmentSearchVo) {
        DepartmentExample departmentExample = new DepartmentExample();
        DepartmentExample.Criteria criteria = departmentExample.createCriteria();
        if (departmentSearchVo.getDepartmentName() != null) {
                criteria.andDepartmentNameLike("%"+departmentSearchVo.getDepartmentName()+"%");
        }
        criteria.andIsDeletedEqualTo(Boolean.valueOf(IsDelete.FALSE.getCode()));
        return departmentMapper.selectByExample(departmentExample);
    }

    /**
     * 根据条件查询部门表
     * @param departmentExample 部门表查询条件
     * @return 部门表列表
     */
    public List<Department> selectByExample(DepartmentExample departmentExample) {
        return departmentMapper.selectByExample(departmentExample);
    }
    /**
     * 根据部门id查询部门树
     * @param departmentId 部门id
     * @return 部门树
     */
    public List<DepartmentTree> getDepartmentTree(String departmentId) {
        //查询所有部门信息
        DepartmentExample departmentExample = new DepartmentExample();
        DepartmentExample.Criteria criteria = departmentExample.createCriteria();
        criteria.andIsDeletedEqualTo(Boolean.valueOf(IsDelete.FALSE.getCode()));
        List<Department> departments = departmentMapper.selectByExample(departmentExample);
        List<DepartmentTree> resultTree = new ArrayList<>();
        List<Department> roots;
        if (departmentId == null){
            roots = departments.stream().filter(department -> department.getPid() == null|| department.getPid().isEmpty())
                    .collect(Collectors.toList());
        }else{
            roots = departments.stream().filter(department -> department.getId().equals(departmentId))
                    .collect(Collectors.toList());
        }
        for (Department root : roots) {
            DepartmentTree departmentTree = new DepartmentTree();
            BeanUtils.copyProperties(root, departmentTree);
            resultTree.add(departmentTree);
        }
        getChildren(departments,resultTree);
        return resultTree;
    }

    /**
     * 递归构建部门树
     * @param departments 所有部门
     * @param resultTree 结果树
     */
    private void getChildren(List<Department> departments, List<DepartmentTree> resultTree){
        //遍历根节点
        for (DepartmentTree departmentTree : resultTree) {
            List<Department> childrens = departments.stream().filter(department ->
                            (department.getPid()!=null&&department.getPid().equals(departmentTree.getId())))
                    .toList();
            if(!childrens.isEmpty()){
                List<DepartmentTree> TmpTreeList = new ArrayList<DepartmentTree>();
                for (Department children : childrens) {
                    DepartmentTree TmpTree = new DepartmentTree();
                    BeanUtils.copyProperties(children, TmpTree);
                    TmpTreeList.add(TmpTree);
                }
                departmentTree.setChildren(TmpTreeList);
                getChildren(departments,TmpTreeList);
            }
        }
    }
}